package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.FacultyDAO;
import edu.unf.cen6940.dao.FacultyresearchDAO;
import edu.unf.cen6940.dao.FacultyresearchtostudentDAO;
import edu.unf.cen6940.dao.StudentDAO;

import edu.unf.cen6940.domain.Faculty;
import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Facultyresearchtostudent;
import edu.unf.cen6940.domain.Student;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Facultyresearchtostudent entities
 * 
 */

@Service("FacultyresearchtostudentService")
@Transactional
public class FacultyresearchtostudentServiceImpl implements
		FacultyresearchtostudentService {

	/**
	 * DAO injected by Spring that manages Faculty entities
	 * 
	 */
	@Autowired
	private FacultyDAO facultyDAO;

	/**
	 * DAO injected by Spring that manages Facultyresearch entities
	 * 
	 */
	@Autowired
	private FacultyresearchDAO facultyresearchDAO;

	/**
	 * DAO injected by Spring that manages Facultyresearchtostudent entities
	 * 
	 */
	@Autowired
	private FacultyresearchtostudentDAO facultyresearchtostudentDAO;

	/**
	 * DAO injected by Spring that manages Student entities
	 * 
	 */
	@Autowired
	private StudentDAO studentDAO;

	/**
	 * Instantiates a new FacultyresearchtostudentServiceImpl.
	 *
	 */
	public FacultyresearchtostudentServiceImpl() {
	}

	/**
	 * Delete an existing Student entity
	 * 
	 */
	@Transactional
	public Facultyresearchtostudent deleteFacultyresearchtostudentStudent(Integer facultyresearchtostudent_facultyResearchId, Integer facultyresearchtostudent_studentId, Integer facultyresearchtostudent_facultyId, Integer related_student_studentId) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyresearchtostudent_facultyResearchId, facultyresearchtostudent_studentId, facultyresearchtostudent_facultyId, -1, -1);
		Student related_student = studentDAO.findStudentByPrimaryKey(related_student_studentId, -1, -1);

		facultyresearchtostudent.setStudent(null);
		related_student.getFacultyresearchtostudents().remove(facultyresearchtostudent);
		facultyresearchtostudent = facultyresearchtostudentDAO.store(facultyresearchtostudent);
		facultyresearchtostudentDAO.flush();

		related_student = studentDAO.store(related_student);
		studentDAO.flush();

		studentDAO.remove(related_student);
		studentDAO.flush();

		return facultyresearchtostudent;
	}

	/**
	 * Delete an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public Facultyresearchtostudent deleteFacultyresearchtostudentFacultyresearch(Integer facultyresearchtostudent_facultyResearchId, Integer facultyresearchtostudent_studentId, Integer facultyresearchtostudent_facultyId, Integer related_facultyresearch_facultyResearchId) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyresearchtostudent_facultyResearchId, facultyresearchtostudent_studentId, facultyresearchtostudent_facultyId, -1, -1);
		Facultyresearch related_facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearch_facultyResearchId, -1, -1);

		facultyresearchtostudent.setFacultyresearch(null);
		related_facultyresearch.getFacultyresearchtostudents().remove(facultyresearchtostudent);
		facultyresearchtostudent = facultyresearchtostudentDAO.store(facultyresearchtostudent);
		facultyresearchtostudentDAO.flush();

		related_facultyresearch = facultyresearchDAO.store(related_facultyresearch);
		facultyresearchDAO.flush();

		facultyresearchDAO.remove(related_facultyresearch);
		facultyresearchDAO.flush();

		return facultyresearchtostudent;
	}

	/**
	 * Return a count of all Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Integer countFacultyresearchtostudents() {
		return ((Long) facultyresearchtostudentDAO.createQuerySingleResult("select count(*) from Facultyresearchtostudent o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Facultyresearchtostudent deleteFacultyresearchtostudentFaculty(Integer facultyresearchtostudent_facultyResearchId, Integer facultyresearchtostudent_studentId, Integer facultyresearchtostudent_facultyId, Integer related_faculty_facultyId) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyresearchtostudent_facultyResearchId, facultyresearchtostudent_studentId, facultyresearchtostudent_facultyId, -1, -1);
		Faculty related_faculty = facultyDAO.findFacultyByPrimaryKey(related_faculty_facultyId, -1, -1);

		facultyresearchtostudent.setFaculty(null);
		related_faculty.getFacultyresearchtostudents().remove(facultyresearchtostudent);
		facultyresearchtostudent = facultyresearchtostudentDAO.store(facultyresearchtostudent);
		facultyresearchtostudentDAO.flush();

		related_faculty = facultyDAO.store(related_faculty);
		facultyDAO.flush();

		facultyDAO.remove(related_faculty);
		facultyDAO.flush();

		return facultyresearchtostudent;
	}

	/**
	 * Return all Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public List<Facultyresearchtostudent> findAllFacultyresearchtostudents(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Facultyresearchtostudent>(facultyresearchtostudentDAO.findAllFacultyresearchtostudents(startResult, maxRows));
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Facultyresearchtostudent saveFacultyresearchtostudentFaculty(Integer facultyResearchId, Integer studentId, Integer facultyId, Faculty related_faculty) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyResearchId, studentId, facultyId, -1, -1);
		Faculty existingfaculty = facultyDAO.findFacultyByPrimaryKey(related_faculty.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfaculty != null) {
			existingfaculty.setFacultyId(related_faculty.getFacultyId());
			existingfaculty.setNnumber(related_faculty.getNnumber());
			existingfaculty.setFirstName(related_faculty.getFirstName());
			existingfaculty.setLastName(related_faculty.getLastName());
			existingfaculty.setEmail(related_faculty.getEmail());
			existingfaculty.setUrl(related_faculty.getUrl());
			existingfaculty.setActive(related_faculty.getActive());
			related_faculty = existingfaculty;
		}

		facultyresearchtostudent.setFaculty(related_faculty);
		related_faculty.getFacultyresearchtostudents().add(facultyresearchtostudent);
		facultyresearchtostudent = facultyresearchtostudentDAO.store(facultyresearchtostudent);
		facultyresearchtostudentDAO.flush();

		related_faculty = facultyDAO.store(related_faculty);
		facultyDAO.flush();

		return facultyresearchtostudent;
	}

	/**
	 * Save an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public void saveFacultyresearchtostudent(Facultyresearchtostudent facultyresearchtostudent) {
		Facultyresearchtostudent existingFacultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyresearchtostudent.getFacultyResearchId(), facultyresearchtostudent.getStudentId(), facultyresearchtostudent.getFacultyId());

		if (existingFacultyresearchtostudent != null) {
			if (existingFacultyresearchtostudent != facultyresearchtostudent) {
				existingFacultyresearchtostudent.setFacultyResearchId(facultyresearchtostudent.getFacultyResearchId());
				existingFacultyresearchtostudent.setStudentId(facultyresearchtostudent.getStudentId());
				existingFacultyresearchtostudent.setFacultyId(facultyresearchtostudent.getFacultyId());
			}
			facultyresearchtostudent = facultyresearchtostudentDAO.store(existingFacultyresearchtostudent);
		} else {
			facultyresearchtostudent = facultyresearchtostudentDAO.store(facultyresearchtostudent);
		}
		facultyresearchtostudentDAO.flush();
	}

	/**
	 */
	@Transactional
	public Facultyresearchtostudent findFacultyresearchtostudentByPrimaryKey(Integer facultyResearchId, Integer studentId, Integer facultyId) {
		return facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyResearchId, studentId, facultyId);
	}

	/**
	 * Save an existing Student entity
	 * 
	 */
	@Transactional
	public Facultyresearchtostudent saveFacultyresearchtostudentStudent(Integer facultyResearchId, Integer studentId, Integer facultyId, Student related_student) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyResearchId, studentId, facultyId, -1, -1);
		Student existingstudent = studentDAO.findStudentByPrimaryKey(related_student.getStudentId());

		// copy into the existing record to preserve existing relationships
		if (existingstudent != null) {
			existingstudent.setStudentId(related_student.getStudentId());
			existingstudent.setNnumber(related_student.getNnumber());
			existingstudent.setFirstName(related_student.getFirstName());
			existingstudent.setLastName(related_student.getLastName());
			existingstudent.setEmail(related_student.getEmail());
			existingstudent.setMajor(related_student.getMajor());
			existingstudent.setGraduated(related_student.getGraduated());
			existingstudent.setCurrentPosition(related_student.getCurrentPosition());
			existingstudent.setYearGraduated(related_student.getYearGraduated());
			related_student = existingstudent;
		}

		facultyresearchtostudent.setStudent(related_student);
		related_student.getFacultyresearchtostudents().add(facultyresearchtostudent);
		facultyresearchtostudent = facultyresearchtostudentDAO.store(facultyresearchtostudent);
		facultyresearchtostudentDAO.flush();

		related_student = studentDAO.store(related_student);
		studentDAO.flush();

		return facultyresearchtostudent;
	}

	/**
	 * Load an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Set<Facultyresearchtostudent> loadFacultyresearchtostudents() {
		return facultyresearchtostudentDAO.findAllFacultyresearchtostudents();
	}

	/**
	 * Save an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public Facultyresearchtostudent saveFacultyresearchtostudentFacultyresearch(Integer facultyResearchId, Integer studentId, Integer facultyId, Facultyresearch related_facultyresearch) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyResearchId, studentId, facultyId, -1, -1);
		Facultyresearch existingfacultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearch.getFacultyResearchId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyresearch != null) {
			existingfacultyresearch.setFacultyResearchId(related_facultyresearch.getFacultyResearchId());
			existingfacultyresearch.setTitle(related_facultyresearch.getTitle());
			existingfacultyresearch.setResearchDescription(related_facultyresearch.getResearchDescription());
			existingfacultyresearch.setResearchStatus(related_facultyresearch.getResearchStatus());
			existingfacultyresearch.setMinumumRequirements(related_facultyresearch.getMinumumRequirements());
			related_facultyresearch = existingfacultyresearch;
		}

		facultyresearchtostudent.setFacultyresearch(related_facultyresearch);
		related_facultyresearch.getFacultyresearchtostudents().add(facultyresearchtostudent);
		facultyresearchtostudent = facultyresearchtostudentDAO.store(facultyresearchtostudent);
		facultyresearchtostudentDAO.flush();

		related_facultyresearch = facultyresearchDAO.store(related_facultyresearch);
		facultyresearchDAO.flush();

		return facultyresearchtostudent;
	}

	/**
	 * Delete an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public void deleteFacultyresearchtostudent(Facultyresearchtostudent facultyresearchtostudent) {
		facultyresearchtostudentDAO.remove(facultyresearchtostudent);
		facultyresearchtostudentDAO.flush();
	}
}
